<?php
/**
 * 权限管理控制器
 * PHP version 5
 * @package   Antonia
 * @author    Antonia <471699715@qq.com>
 * @copyright 2016 Antonia.
 */
namespace Admin\Controller;
use Think;
class AuthController extends AdmincommonController {
    /**
     * 显示列表
     * @param HttpRequest $request
     * @return html
     * @author Antonia
     */
    public function index() {
        $auth = D("Auths");
        $_GET['p'] = $_POST['p'];
        if (!empty ($_POST['orderField'] )) {
            $orderField = $_POST['orderField'];
        } else {
            $orderField = ! empty ( $sortBy ) ? $sortBy : $auth->getPk();
        }
        if (isset ( $_POST['orderDirection'] )) {
            $orderDirection = $_POST['orderDirection'] == 'asc' ? 'asc' : 'desc';
        } else {
            $orderDirection = 'desc';
        }
        $count = $auth->where('parent_id=0')->count();
        if (!empty($_POST['numPerPage'])){
            $numPerPage = $_POST['numPerPage'];
        } else {
            $numPerPage = 20;
        }
        $Page  =  new \Think\Page($count,$numPerPage);
        $list  = $auth->order('sortNum asc,' . $orderField . ' ' . $orderDirection)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->field('auth_id,parent_id,title,controller,action,sortNum')
            ->where('parent_id=0')
            ->select();
        if($list) {
            $newlist = array();
            foreach($list as $l) {
                $chirdren = $auth->order('sortNum asc, auth_id desc')
                            ->field('auth_id,title,controller,action,sortNum')
                            ->where('parent_id=' . $l['auth_id'])
                            ->select(); 
                if(!$chirdren) {
                    $chirdren = array();
                }         
                $l['chird'] = $chirdren;
                $newlist[] = $l;
            }
        }
        $this->assign('list',$newlist);
        $this->assign ('orderDirection', $orderDirection);
        $this->assign ('orderField', $orderField);
        $this->assign('filters', $_POST);
        $this->assign ('totalCount', $count);
        $this->assign ('numPerPage', $Page->listRows);
        $this->assign ('currentPage', !empty($_GET['p'])?$_GET['p']:1);
        $this->display();
    }

    /**
     * 显示新增管理员页面
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function add() {
        $Auths = D("Auths");
        $this->assign('auths', $Auths->getOptions());
        $this->display('info');
    }

    /**
     * 处理新增
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function insert() {
        $auth = D("Auths");
        if ($vo = $auth->create()) {
            unset($vo["auth_id"]);
            $rs = $auth->add($vo);
            if ($rs !== false) {
                $re['statusCode'] = "200";
                $re['message'] = "新增成功!";
                $re['navTabId'] = "Authindex";
                $re['callbackType'] = "closeCurrent";
            } else {
                $re['statusCode'] = "300";
                $re['message'] = "新增错误，请重试!";
            }
        } else {
                $re['statusCode'] = "300";
                $re['message'] = $auth->getError();
        }
		
        echo json_encode($re);
    }

    /**
     * 显示编辑页面
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function edit() {
        $auth_id = remove_xss($_GET['id']);
        if (!empty($auth_id)) {
            $Auths     = D("Auths");
            $authInfo  = $Auths->find($auth_id);
            if ($authInfo) {
                $this->assign('auths', $Auths->getOptions());
                $this->assign('authinfo', $authInfo);
                $this->display('info');
            } else {
                $re['statusCode'] = "300";
                $re['navTabId'] = "Authindex";
                $re['callbackType'] = "closeCurrent";
                $re['message'] = "该记录不存在！";
                echo json_encode($re);
            }
        } else {
                $re['statusCode'] = "300";
                $re['navTabId'] = "Authindex";
                $re['callbackType'] = "closeCurrent";
                $re['message'] = "该记录不存在！";
                echo json_encode($re);
        }
    }

    /**
     * 处理更新数据
     * @param HttpRequest $request
     * @return void
     * @author Fee
    */
    public function update() {
        $auth = D("Auths");
        if ($vo = $auth->create()) {
            $list = $auth->save($vo);
            if ($list !== false) {
                $re['statusCode'] = "200";
                $re['message'] = "修改成功!";
                $re['navTabId'] = "Authindex";
                $re['callbackType'] = "closeCurrent";
            } else {
                $re['statusCode'] = "300";
                $re['message'] = "没有更新任何数据!";
            }
        } else {
                $re['statusCode'] = "300";
                $re['message'] = $auth->getError();
        }
        echo json_encode($re);
    }

    /**
     * 删除管理员
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function delete() {
        if (!empty($_GET['id'])) {
            $auth = D("Auths");
            $result = $auth->delete(remove_xss($_GET['id']));
            if (false !== $result) {
                $re['statusCode'] = "200";
                $re['message'] = "删除成功!";
            } else {
                $re['statusCode'] = "300";
                $re['message'] = "删除失败，请重试!";
            }
        } else {
                $re['statusCode'] = "300";
                $re['message'] = "该记录不存在！";;
        }
        echo json_encode($re);
    }

    /**
     * 删除选定内容
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function delAll() {
        $auth = D("Auths");
        $map['auth_id']  = array('in', explode(',', $_POST["ids"]));
        $result  = $auth->where($map)->delete();
        if($result) {
            $re['statusCode'] = "200";
            $re['message'] = "删除成功!";
        } else {
            $re['statusCode'] = "300";
            $re['message'] = "删除失败，请重试!";
        }
        echo json_encode($re);
    }
}
?>